package com.example.boot1;
//当前线程main
//ThreadLocal 是操作线程中ThreadLocalMap的工具类
//ThreadLocalMap变量中存储存放的threadLocal为key,传进去的值为value (10) (xxx)
//线程隔离
// set 方法将变量绑定到线程中
// get 获取当前线程绑定的变量
public class ThreadLocalDemo {

    ThreadLocal<String> t1 = new ThreadLocal<>();

    //变量
    private String context;

    public String getContext() {
        //return context;
        String s = t1.get();
        return s;
    }

    public void setContext(String context) {
        //this.context = context;
        t1.set(context);
    }

    public static void main(String[] args) {
        ThreadLocalDemo threadLocalDemo = new ThreadLocalDemo();

        for (int i = 1; i <= 5; i++) {
            new Thread(() -> {
                //每一个线程存一个变量，过一会，取出
                threadLocalDemo.setContext(Thread.currentThread().getName()+"\t 的数据");
                System.out.println("-----------------------");
                System.out.println(Thread.currentThread().getName()+"\t ---> "+threadLocalDemo.getContext());
            }, "线程"+i).start();
        }
    }
}
